Raise MessageParseError (not TypeError) on malformed field types#988
Open
Zeffut wants to merge 1 commit into
Open
Raise MessageParseError (not TypeError) on malformed field types#988Zeffut wants to merge 1 commit into
Zeffut wants to merge 1 commit into
Conversation
…oads parse_message documents that malformed messages raise MessageParseError, but each case-branch's try/except only catches KeyError. When a field is present but of the wrong type (e.g. rate_limit_event with rate_limit_info=None, or user/assistant with message=None), the subsequent indexing raises TypeError, which escapes the parser and crashes the read loop -- losing every subsequent message in the stream. Add a TypeError clause alongside the existing KeyError clause in the six case branches that index sub-fields. Existing "Missing required field..." wording is preserved for KeyError (backward-compatible with existing tests); the new clause emits "Malformed field..." through the same MessageParseError type and carries the original payload. Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR hardens message parsing so malformed/legacy CLI payloads raise MessageParseError (with original data attached) instead of leaking TypeError and potentially crashing the parser loop.
Changes:
- Added regression tests for malformed
rate_limit_eventpayloads and non-dictmessagefields foruser/assistant. - Updated
parse_messageto catchTypeErrorin multiple message-type branches and re-raise asMessageParseError.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/test_message_parser.py | Adds test coverage to ensure malformed payloads raise MessageParseError rather than TypeError. |
| src/claude_agent_sdk/_internal/message_parser.py | Converts TypeError during parsing into MessageParseError across supported message types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+253
to
+256
| except TypeError as e: | ||
| raise MessageParseError( | ||
| f"Malformed field in system message: {e}", data | ||
| ) from e |
Comment on lines
+125
to
+128
| except TypeError as e: | ||
| raise MessageParseError( | ||
| f"Malformed field in user message: {e}", data | ||
| ) from e |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
parse_messagein_internal/message_parser.pydocuments that "malformed input" should raiseMessageParseError, but the sixcasebranches only catchKeyError. A payload with a field of the wrong type (e.g.rate_limit_eventwithrate_limit_info=None, or anyuser/assistant/system/result/stream_eventmessage where a sub-field is missing structure) leaks a bareTypeError. Because the parser is called inside the consumer's read loop, that escapes the loop and silently drops every subsequent message on the stream.Change
except TypeErrorto each of the sixcasebranches, raisingMessageParseError("Malformed field in <type> message: ...", data=payload).KeyError → "Missing required field..."messages verbatim so existing test assertions remain valid.tests/test_message_parser.pycoveringrate_limit_info∈ {None, str, int} anduser/assistantmessage=None.Verification
pytest tests/test_message_parser.py: 57 pass (54 prior + 3 new)pytest tests/: 753 pass, 3 skippedruff check,ruff format --check,mypy src/: clean